package com.gitee.mazhenggg.mall.point.web.controller;

import com.gitee.mazhenggg.mall.point.domain.type.UserId;
import com.gitee.mazhenggg.mall.point.domain.type.Value;
import com.gitee.mazhenggg.mall.point.web.service.PointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping(value = "/api/point")
public class PointController {

    @Autowired
    PointService pointService;

    @GetMapping(value="/{userId}")
    public Long getPointByUserId(@PathVariable Long userId){
        Value point = pointService.getPointByUserId(new UserId(userId));
        return point.getValue();
    }

    @PostMapping(value="/add")
    public Boolean addPoint(@RequestBody Map<String, Long> pointMap) {
        Long userId =pointMap.get("userId");
        Long amount = pointMap.get("amount");
        pointService.updatePointByUserId(new UserId(userId),new Value(amount));
        return true;
    }
}
